package com.xjj.datarources.common;

import com.xjj.datarources.exception.APIException;
import org.apache.shiro.authz.UnauthorizedException;
import org.springframework.validation.BindException;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @name: xjj
 * @date: 2021/5/20 10:01
 *
 *
 *   全局异常处理
 *
 */
@RestControllerAdvice
public class ControllerExceptionAdvice {


    /**
     * 统一校验
     */

    @ExceptionHandler({BindException.class})
    public ResultVo validExceptionHandler(BindException bind){

        ObjectError objectError = bind.getBindingResult().getAllErrors().get(0);

        return new ResultVo(ResultCode.VALIDATE_ERROR, objectError.getDefaultMessage());

    }

    @ExceptionHandler({UnauthorizedException.class})
    public ResultVo noPermission(UnauthorizedException e){
        return new ResultVo(ResultCode.NO_PERMISSION);
    }

    /**
     * 自定义统一异常
     */

    @ExceptionHandler({APIException.class})
    public ResultVo APIExceptionHandler(APIException e){
        return new ResultVo(e.getCode(), e.getMsg(), e.getMessage());
    }


}
